function GearBuddyOnLoad(self)
	print ("Loading GearBuddy...");
	self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED");
	self:RegisterEvent("PLAYER_TARGET_CHANGED");
	self:RegisterEvent("INSPECT_READY");
end

function GearBuddyOnEvent(self, event, ...)
	if (event == "PLAYER_EQUIPMENT_CHANGED") then
		iLvlPlayer();
	elseif (event == "INSPECT_READY") then
		--first check that the overlay is a child of the inspect frame
		--seems that the parent is not set correctly at boot time
		local overlayParent = iLvlInspectOverlay:GetParent();
		if (overlayParent ~= "InspectPaperDollItemsFrame") then
			iLvlInspectOverlay:SetParent("InspectPaperDollItemsFrame");
			InspectHeadSlotiLvlText:SetPoint("TOPLEFT",InspectHeadSlot,"TOPLEFT",3,-3);
			InspectNeckSlotiLvlText:SetPoint("TOPLEFT",InspectNeckSlot,"TOPLEFT",3,-3);
			InspectShoulderSlotiLvlText:SetPoint("TOPLEFT",InspectShoulderSlot,"TOPLEFT",3,-3);
			InspectBackSlotiLvlText:SetPoint("TOPLEFT",InspectBackSlot,"TOPLEFT",3,-3);
			InspectChestSlotiLvlText:SetPoint("TOPLEFT",InspectChestSlot,"TOPLEFT",3,-3);
			InspectWristSlotiLvlText:SetPoint("TOPLEFT",InspectWristSlot,"TOPLEFT",3,-3);
			InspectHandsSlotiLvlText:SetPoint("TOPLEFT",InspectHandsSlot,"TOPLEFT",3,-3);
			InspectWaistSlotiLvlText:SetPoint("TOPLEFT",InspectWaistSlot,"TOPLEFT",3,-3);
			InspectLegsSlotiLvlText:SetPoint("TOPLEFT",InspectLegsSlot,"TOPLEFT",3,-3);
			InspectFeetSlotiLvlText:SetPoint("TOPLEFT",InspectFeetSlot,"TOPLEFT",3,-3);
			InspectFinger0SlotiLvlText:SetPoint("TOPLEFT",InspectFinger0Slot,"TOPLEFT",3,-3);
			InspectFinger1SlotiLvlText:SetPoint("TOPLEFT",InspectFinger1Slot,"TOPLEFT",3,-3);
			InspectTrinket0SlotiLvlText:SetPoint("TOPLEFT",InspectTrinket0Slot,"TOPLEFT",3,-3);
			InspectTrinket1SlotiLvlText:SetPoint("TOPLEFT",InspectTrinket1Slot,"TOPLEFT",3,-3);
			InspectMainHandSlotiLvlText:SetPoint("TOPLEFT",InspectMainHandSlot,"TOPLEFT",3,-3);
			InspectSecondaryHandSlotiLvlText:SetPoint("TOPLEFT",InspectSecondaryHandSlot,"TOPLEFT",3,-3);
			InspectRangedSlotiLvlText:SetPoint("TOPLEFT",InspectRangedSlot,"TOPLEFT",3,-3);
			InspectAverageiLvlText:SetPoint("TOPLEFT",InspectRangedSlot,"TOPLEFT",-120,27);
		end
		--print ("Received INSPECT_READY event");
		NotifyInspect("target");
		iLvlTarget();
	elseif (event == "PLAYER_TARGET_CHANGED") then
		--iLvlTarget("clear");
		if (iLvlInspectOverlay:IsVisible()) then
			NotifyInspect("target");
			iLvlTarget();
		end
	end
end

function iLvlTarget(clear)
	if (clear == "clear") then
		local itemLevelBySlot = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	end

	--print ("Inspect overlay should now be visible");
	local itemLevelBySlot = calciLvl("target");	
	local avgiLvl = itemLevelBySlot[19];

	InspectHeadSlotiLvlText:SetFormattedText(itemLevelBySlot[1]);
	InspectNeckSlotiLvlText:SetFormattedText(itemLevelBySlot[2]);
        InspectShoulderSlotiLvlText:SetFormattedText(itemLevelBySlot[3]);
        InspectBackSlotiLvlText:SetFormattedText(itemLevelBySlot[15]);
        InspectChestSlotiLvlText:SetFormattedText(itemLevelBySlot[5]);
        InspectWristSlotiLvlText:SetFormattedText(itemLevelBySlot[9]);
        InspectHandsSlotiLvlText:SetFormattedText(itemLevelBySlot[10]);
        InspectWaistSlotiLvlText:SetFormattedText(itemLevelBySlot[6]);
        InspectLegsSlotiLvlText:SetFormattedText(itemLevelBySlot[7]);
        InspectFeetSlotiLvlText:SetFormattedText(itemLevelBySlot[8]);
        InspectFinger0SlotiLvlText:SetFormattedText(itemLevelBySlot[11]);
        InspectFinger1SlotiLvlText:SetFormattedText(itemLevelBySlot[12]);
        InspectTrinket0SlotiLvlText:SetFormattedText(itemLevelBySlot[13]);
        InspectTrinket1SlotiLvlText:SetFormattedText(itemLevelBySlot[14]);
        InspectMainHandSlotiLvlText:SetFormattedText(itemLevelBySlot[16]);
        InspectSecondaryHandSlotiLvlText:SetFormattedText(itemLevelBySlot[17]);
        InspectRangedSlotiLvlText:SetFormattedText(itemLevelBySlot[18]);
	InspectAverageiLvlText:SetFormattedText("Average iLvl (equipped): " .. avgiLvl);
end

function calciLvl(mode)
	local invSlot; -- 1 to 18
        local itemID;
        local itemSubtype;
        local totaliLvl = 0;
        local numSlots = 17; -- for calculating the average
        local itemNamesBySlot = {};
        local itemLevelBySlot = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        for invSlot=1,18 do
                itemID = GetInventoryItemLink(mode,invSlot);
                if itemID then
                        local itemName,_,_,itemLevel,_,_,itemSubtype,_,_,_,_ = GetItemInfo(itemID);
                        itemNamesBySlot[invSlot] = itemName;
                        itemLevelBySlot[invSlot] = itemLevel;
                        --print (mode .. " - Found " .. itemName .. " with ilvl " .. itemLevel .. " in slot " .. invSlot);
                        if (invSlot == 16) then
                                --if Main Hand is two-hander, only divide by 16 (offhand will be zero - fury warriors taken care of below)
                                if (string.match(itemSubtype,"Two") or string.match(itemSubtype,"Stave") or string.match(itemSubtype,"Polearm") or string.match(itemSubtype,"Fishing")) then
                                        --print ("Found two handed weapon");
                                        numSlots = 16;
                                end
                                totaliLvl = totaliLvl + itemLevel;
                        elseif (invSlot == 17) then
                                --if we have something in this slot then set numslots to 17
                                --even if we had set it to 16 by finding a two-hander in the main-hand
                                --takes care of warrriors dual-wielding two-handers
                                numSlots = 17;
                                totaliLvl = totaliLvl + itemLevel;
                        elseif (invSlot ~= 4) then
                                --Shirt is in slot 4 - skip it for iLvl calculations
				if itemLevel then
                                	totaliLvl = totaliLvl + itemLevel;
				end
                        end
                else
                        itemNamesBySlot[invSlot] = "Missing";
                        itemLevelBySlot[invSlot] = 0;
                        --print (mode .. " - Skipping slot " .. invSlot .. " (Nothing there)");
                end
        end
        itemLevelBySlot[19] = math.floor(totaliLvl/numSlots);
	return itemLevelBySlot;	
end

function iLvlPlayer()
	--Our frame is a child of the paperdoll frame and each slot now has an overlay text box to hold the 
	--ilvl of the item in its slot
	--print ("Running iLvl for " .. mode);
	local itemLevelBySlot = calciLvl("player");
	local avgiLvl = itemLevelBySlot[19];

	HeadSlotiLvlText:SetFormattedText(itemLevelBySlot[1]);
	NeckSlotiLvlText:SetFormattedText(itemLevelBySlot[2]);
	ShoulderSlotiLvlText:SetFormattedText(itemLevelBySlot[3]);
	BackSlotiLvlText:SetFormattedText(itemLevelBySlot[15]);
	ChestSlotiLvlText:SetFormattedText(itemLevelBySlot[5]);
	WristSlotiLvlText:SetFormattedText(itemLevelBySlot[9]);
	HandsSlotiLvlText:SetFormattedText(itemLevelBySlot[10]);
	WaistSlotiLvlText:SetFormattedText(itemLevelBySlot[6]);
	LegsSlotiLvlText:SetFormattedText(itemLevelBySlot[7]);
	FeetSlotiLvlText:SetFormattedText(itemLevelBySlot[8]);
	Finger0SlotiLvlText:SetFormattedText(itemLevelBySlot[11]);
	Finger1SlotiLvlText:SetFormattedText(itemLevelBySlot[12]);
	Trinket0SlotiLvlText:SetFormattedText(itemLevelBySlot[13]);
	Trinket1SlotiLvlText:SetFormattedText(itemLevelBySlot[14]);
	MainHandSlotiLvlText:SetFormattedText(itemLevelBySlot[16]);
	SecondaryHandSlotiLvlText:SetFormattedText(itemLevelBySlot[17]);
	RangedSlotiLvlText:SetFormattedText(itemLevelBySlot[18]);
	averageiLvlText:SetFormattedText("Average iLvl (equipped): " .. avgiLvl);
end
